﻿@using StackExchange.DataExplorer
@using StackExchange.DataExplorer.Helpers;
@using System.Reflection;
@{
    this.SetPageTitle("Stack Exchange Data Explorer Administration - Stack Exchange Data Explorer");

    var cachedTables = HelperTableCache.GetCachedTables();
}
<h3>Site Information</h3>
<ul>
    <li><a class="post-action" href="/admin/refresh-stats">Update site statistics</a> (will refresh front page stats and purge <strong>all</strong> caches)</li>
</ul>
<h3>User Management</h3>
<ul>
    <li><a href="/admin/whitelist">OpenID whitelist</a></li>
    <li><a href="/admin/find-dupe-users">Find dupe users</a> (<a href="/admin/find-dupe-users?useemail=true">by email</a>)</li>
</ul>
<h3>Application Settings</h3>
<ul>
    <li>There are currently <strong>@ViewData["CacheCount"]</strong> cached results (<a class="post-action" href="/admin/clear-cache">clear all</a>)</li>
    <li>
    @{ var hasTables = false; }
    @if (HelperTableCache.Preferences == null)
    {
        @: Helper table caching is not enabled
    }
    else if (cachedTables.Count == 0)
    {
        @: No tables matched the cache inclusion pattern /@HelperTableCache.Preferences.IncludePattern/
    }
    else
    {
        hasTables = true;
        @: Caching the following <strong>@cachedTables.Count</strong> distinct @("table".Pluralize(@cachedTables.Count)) on a @(HelperTableCache.Preferences.PerSite ? "per-site" : "global") basis
    }
    (<a class="post-action" data-reload="true" href="/admin/clear-table-cache">refresh cache</a>)
    @if (hasTables)
    {
        <ul class="sublist">
        @foreach (var table in cachedTables)
        {
            <li>@table</li>
        }
        </ul>
    }
    </li>
    <li>
        Current AppSettings values (<a class="post-action" data-reload="true" href="/admin/refresh-settings">refresh from database</a>)
        <dl class="tabular-list">
        @foreach (var property in typeof(AppSettings).GetProperties(BindingFlags.Static | BindingFlags.Public))
        {
            var value = (property.GetValue(null, null) ?? "").ToString();
            <dt>@property.Name</dt>
            <dd>
            @if (value.HasValue())
            {
                @value
            }
            else
            {
                <em>(empty)</em>
            }
            </dd>
        }
        </dl>
    </li>
</ul>
@Html.AntiForgeryToken()
<script type="text/javascript">
(function () {
    $('a.post-action').each(function () {
        $(this).on('click', function () {
            var reload = $(this).data('reload'),
                textNode = document.create('span', { text: "processing..." }),
                link = this;

            link.parentNode.insertBefore(textNode, link);
            link.parentNode.removeChild(link);

            $.post(this.href, DataExplorer.token({}), function () {
                if (reload) {
                    window.location.reload(true);
                } else {
                    textNode.parentNode.insertBefore(link, textNode);
                    textNode.parentNode.removeChild(textNode);
                }
            });

            return false;
        });
    });
})();
</script>
